package com.xinxin.facelinker.utils;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.activity.MainActivity;
import com.xinxin.facelinker.domain.Login;
import com.xinxin.facelinker.domain.Search;
import com.xinxin.facelinker.domain.ShowPals;
import com.xinxin.facelinker.domain.Verity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gengliang on 2015/10/31.
 */
public class FaceUtil {
    private static HttpUtils httpUtils = new HttpUtils();
    private static String face_url = "http://apicn.faceplusplus.com";
    private static String api_key = "92a556fe9a7facea0054a3c00760291d";
    private static String api_secret = "uGz_XnKW7Bt-TQYMRL2MuSBNmiHZIvz7";
    private static String result;
    private static Verity verity;
    private static ArrayList<Search> arrayList=new ArrayList<>();

    public static String detect(final Context context, String url) {
        result="";
        face_url = face_url + "/v2/detection/detect";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_URL, url);
//        params.addBodyParameter(Config.KEY_ATTRIBUTE, "glass,pose,gender,age,race,smiling");
        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    JSONArray jsonArray = jsonObject.getJSONArray("face");
                    if (jsonArray.length() > 1) {
                        Toast.makeText(context, "自拍照只能有一张脸", Toast.LENGTH_SHORT).show();
                        return;
                    }

                    if (responseInfo.result.isEmpty()) {
                        Toast.makeText(context, "图片没有检测到人脸，请重新传送", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                    result = jsonObject1.get("face_id").toString();
                } catch (JSONException e) {
                    Toast.makeText(context, "人脸检测失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "人脸检测失败", Toast.LENGTH_SHORT).show();
                return;
            }
        });
        return result;
    }

    public static String createPerson(final Context context) {
        result="";
        face_url = face_url + "/v2/person/create";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    result = jsonObject.get("person_id").toString();
                } catch (JSONException e) {
                    Toast.makeText(context, "创建person失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "创建person失败", Toast.LENGTH_SHORT).show();
                return;
            }
        });
        return result;
    }

    public static void personAddFace(final Context context, String person_id, String face_id) {
        result="";
        face_url = face_url + "/v2/person/add_face";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_PERSON_ID, person_id);
        params.addBodyParameter(Config.KEY_FACE_ID, face_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    if ((boolean) jsonObject.get("success")) {
                        Toast.makeText(context, "添加face到person成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, "添加face到person失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    Toast.makeText(context, "添加face到person失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "添加face到person失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static String createFaceset(final Context context) {
        result="";
        face_url = face_url + "/v2/faceset/create";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    result = jsonObject.get("faceset_id").toString();
                } catch (JSONException e) {
                    Toast.makeText(context, "创建faceset失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "创建faceset失败", Toast.LENGTH_SHORT).show();
                return;
            }
        });
        return result;
    }

    public static void facesetAddFace(final Context context, String faceset_id, String face_id) {
        result="";
        face_url = face_url + "/v2/faceset/add_face";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_FACESET_ID, faceset_id);
        params.addBodyParameter(Config.KEY_FACE_ID, face_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);
                    if ((boolean) jsonObject.get("success")) {
                        Toast.makeText(context, "添加face到faceset成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(context, "添加face到faceset失败", Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    Toast.makeText(context, "添加face到faceset失败", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "添加face到faceset失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static void trainVerify(final Context context, String person_id) {
        result="";
        face_url = face_url + "/v2/train/verify";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_PERSON_ID, person_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Toast.makeText(context, "verity成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "verity成功失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static Verity recoginitionVerity(final Context context, String person_id, String face_id) {
        result="";
        face_url = face_url + "/v2/recognition/verify";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_PERSON_ID, person_id);
        params.addBodyParameter(Config.KEY_FACE_ID, face_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                verity = (Verity) NetHelper.parseJsonData(responseInfo.result, Verity.class);
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "verity成功失败", Toast.LENGTH_SHORT).show();
                return;
            }
        });
        return verity;
    }

    public static void trainSearch(final Context context,String faceset_id){
        result="";
        face_url = face_url + "/v2/train/search";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_FACESET_ID, faceset_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Toast.makeText(context, "search成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "search成功失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static ArrayList recoginitionSearch(final Context context, String faceset_id, String face_id) {
        result="";
        face_url = face_url + "/v2/recognition/search";
        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.KEY_API_KEY, api_key);
        params.addBodyParameter(Config.KEY_API_SECRET, api_secret);
        params.addBodyParameter(Config.KEY_FACESET_ID,faceset_id);
        params.addBodyParameter("key_face_id", face_id);

        httpUtils.send(HttpRequest.HttpMethod.POST, face_url, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Gson gson = new Gson();
                arrayList= gson.fromJson(responseInfo.result, new TypeToken<List<Search>>() {
                }.getType());
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(context, "verity成功失败", Toast.LENGTH_SHORT).show();
                return;
            }
        });
        return arrayList;
    }
}
